﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using HQPad.Models;
using HQPad.DAL;
using HQPad.Security;
using HQPad.Extensions;

namespace HQPad
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new LogonAuthorize());
            filters.Add(new HandleErrorHmAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.png/{*pathInfo}");

            routes.MapRoute(
                "Rules", // Route name
                "admin", // URL with parameters
                new { controller = "Admin", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "ReleaseHistory", // Route name
                "release/history", // URL with parameters
                new { controller = "Release", action = "History" } // Parameter defaults
            );

            routes.MapRoute(
                "Release", // Route name
                "admin/release/{action}/{id}", // URL with parameters
                new { controller = "Release", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );


            routes.MapRoute(
                "InvocationList", // Route name
                "admin/{ruleSetId}/invocations", // URL with parameters
                new { controller = "Invocation", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "InvocationCreate", // Route name
                "admin/{ruleSetId}/invocation/Create", // URL with parameters
                new { controller = "Invocation", action = "Create" } // Parameter defaults
            );

            routes.MapRoute(
                "Invocation", // Route name
                "admin/{ruleSetId}/invocation/{slug}/{action}", // URL with parameters
                new { controller = "Invocation", action = "Edit" } // Parameter defaults
            );

            routes.MapRoute(
                "SpellList", // Route name
                "admin/{ruleSetId}/spells", // URL with parameters
                new { controller = "Spell", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "SpellCreate", // Route name
                "admin/{ruleSetId}/Spell/Create", // URL with parameters
                new { controller = "Spell", action = "Create" } // Parameter defaults
            );

            routes.MapRoute(
                "Spell", // Route name
                "admin/{ruleSetId}/Spell/{slug}/{action}", // URL with parameters
                new { controller = "Spell", action = "Edit" } // Parameter defaults
            );

            routes.MapRoute(
                "SkillList", // Route name
                "admin/{ruleSetId}/skills", // URL with parameters
                new { controller = "Skill", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "SkillCreate", // Route name
                "admin/{ruleSetId}/Skill/Create", // URL with parameters
                new { controller = "Skill", action = "Create" } // Parameter defaults
            );

            routes.MapRoute(
                "Skill", // Route name
                "admin/{ruleSetId}/Skill/{slug}/{action}", // URL with parameters
                new { controller = "Skill", action = "Edit" } // Parameter defaults
            );

            routes.MapRoute(
                "CharacterNew", // Route name
                "character/new", // URL with parameters
                new { controller = "Character", action = "New" } // Parameter defaults
            );

            routes.MapRoute(
                "CharacterAction", // Route name
                "character/{name}/{action}/{id}", // URL with parameters
                new { controller = "Character", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            /*routes.MapRoute(
                "SkillSubAction", // Route name
                "admin/{ruleSetId}/Skill/{slug}/{action}/{id}", // URL with parameters
                new { controller = "Skill", action = "Edit" } // Parameter defaults
            );*/

            routes.MapRoute(
                "UserCharacters", // Route name
                "admin/user/{action}/{userName}", // URL with parameters
                new { controller = "User", action = "characters" } // Parameter defaults
            );

            // do I need this?
            /*routes.MapRoute(
                "AdminDefault", // Route name
                "admin/{controller}/{action}", // URL with parameters
                new { controller = "Admin", action = "Index" } // Parameter defaults
            );*/

            routes.MapRoute(
                "Dashboard", // Route name
                "dashboard", // URL with parameters
                new { controller = "Home", action = "Dashboard" } // Parameter defaults
            );

            routes.MapRoute(
                "About", // Route name
                "about-hqpad", // URL with parameters
                new { controller = "Home", action = "About" } // Parameter defaults
            );

            /*routes.MapRoute(
                "ImportAction", // Route name
                "import/{action}", // URL with parameters
                new { controller = "Import", action = "Index" } // Parameter defaults
            );
  
            routes.MapRoute(
                "ImportHome", // Route name
                "import", // URL with parameters
                new { controller = "Import", action = "Index" } // Parameter defaults
            );*/

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );



            /*routes.MapRoute(
                "Skills", // Route name
                "admin/Skill/{action}/{id}", // URL with parameters
                new { controller = "Skill", action = "Index", id = 0 } // Parameter defaults
            );*/
        }

        protected void Application_Start()
        {
            ViewEngines.Engines.Add(new HQPad.Extensions.HQPadViewEngine()); 

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

#if DEBUG
            Database.SetInitializer(new HQInitializer()); // load/refresh db content.
#else
            Database.SetInitializer<HQContext>(null);
#endif
        }
    }
}